import React, { Component } from 'react'
import Header from '../../components/Header/Header'
import Goods from './components/Goods/Goods'
import NewPeople from './components/NewPeople/NewPeople'
import Group from './components/Group/Group'
import {reqgetnew,reqgetgroupon,reqgetindexgoods} from '../../http/api'
export default class Home extends Component {
  constructor(){
    super()
    this.state={
      newList:[],
      groupList:[],
      indexGoodsList:[],
      n:0,
    }
  }
  
  changeN(n){
      this.setState({
        n,
      })
  }
  async getnewList(){
    let {data:res} = await reqgetnew();
    if(res.code === 200){
      let arr = res.list ? res.list :[];
      this.setState({
        newList:arr,
      })
      console.log(res);
    }
  }
  async getgroupList(){
    let {data:res} = await reqgetgroupon()
    if(res.code == 200){
      // console.log('group',res.list);
      let arr = res.list ? res.list : [];
      this.setState({
        groupList:arr,
      })
    }
  }
  async getindexgoodsList(){
    let {data:res} = await reqgetindexgoods();
    if(res.code ===200){
      let list = res.list;
      let arr0 = list[0].content ? list[0].content : [];
      let arr1 = list[1].content ? list[1].content : [];
      let arr2 = list[2].content ? list[2].content : [];
      let arr3 = list[3].content ? list[3].content : [];
      this.setState({
         indexGoodsList:[arr0,arr1,arr2,arr3],
      })
    }
  }
  componentDidMount(){
    //请求新人专享数据
    this.getnewList();
    //请求万人团数据
    this.getgroupList();
    //请求首页分类商品信息
    this.getindexgoodsList();
  }
  render() {
    return (
      <div className='homeBox'>
        <Header title="首页"></Header>
        <NewPeople newList={this.state.newList}></NewPeople>
        <Group groupList={this.state.groupList}></Group>
        <Goods changeN={(n)=>this.changeN(n)} indexGoodsList={this.state.indexGoodsList} n={this.state.n}></Goods>
      </div>
    )
  }
}
